package com.applet.mallappletbackstage.controller;

import com.applet.mallappletbackstage.entity.Message;
import com.applet.mallappletbackstage.service.MessageService;
import com.applet.mallappletbackstage.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    MessageService messageService;

    /**
     * 用户获取消息
     *
     * @param id
     * @return
     */
    @GetMapping("list/{id}")
    public R getMessageForUser(@PathVariable String id, @RequestParam int watched) {
        QueryWrapper<Message> messageQueryWrapper = new QueryWrapper<>();
        messageQueryWrapper.eq("user", id);
        messageQueryWrapper.eq("watched", watched);
        List<Message> list = messageService.list(messageQueryWrapper);
        return R.success(list);
    }

    @PostMapping("save")
    public R saveMessage(@RequestBody Message message) {
        messageService.save(message);
        return R.success();
    }

    @PutMapping("watched")
    public R watched(@RequestBody Message message) {
        messageService.updateById(message);
        return R.success();
    }

    @DeleteMapping("delete/{id}")
    public R deleteMessage(@PathVariable String id) {
        messageService.removeById(id);
        return R.success();
    }

    /**
     * 获取未读取的条数
     *
     * @param id
     * @return
     */
    @GetMapping("count/{id}")
    public R getCount(@PathVariable String id) {
        int total = messageService.getCount(id);
        return R.success(total);
    }
}
